from django.db import models
from apps.system.models import Option, Upload
from apps.product.models import Product

# Create your models here.
from utils.exceptions import CustomError


class AdornPedia(models.Model):
    category = models.ForeignKey(Option, verbose_name='分类', on_delete=models.PROTECT)
    name = models.CharField(verbose_name='名称', max_length=50)
    icon = models.CharField(verbose_name='图标', max_length=50)
    color = models.CharField(verbose_name='颜色', max_length=50)
    thumbnail = models.ForeignKey(Upload, verbose_name='缩略图', on_delete=models.PROTECT,
                                  related_name='adorn_pedia_thumbnail')
    abstract = models.CharField(verbose_name='摘要', max_length=250, help_text='多条摘要用|分开', null=True, blank=True)
    content = models.TextField(verbose_name='百科', null=True, blank=True)
    relate_products = models.ManyToManyField(Product, verbose_name='关联产品', blank=True)

    class Meta:
        db_table = 'adorn_pedia'
        verbose_name = '装修百科'

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的装修流程id')

        instance = AdornPedia.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的装修流程实例')
        return instance


class AdornPediaImgs(models.Model):
    adorn_pedia = models.ForeignKey(AdornPedia, verbose_name='装修百科', on_delete=models.PROTECT)
    image = models.ForeignKey(Upload, verbose_name='图片', on_delete=models.PROTECT)

    class Meta:
        db_table = 'adorn_pedia_imgs'
        verbose_name = '装修百科图片'
